package com.example.xwadmin.api.ai;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.xw.common.entity.Comment;
import com.example.xw.common.entity.Weibo;
import com.example.xw.common.vo.Result;
import com.example.xwweibo.mapper.CommentMapper;
import com.example.xwweibo.mapper.WeiboMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Component
public class AI {
    @Autowired
    private WeiboMapper weiboMapper;

    @Scheduled(initialDelay = 5000,fixedDelay = 100000)
    public Result<List<Weibo>> weiboVerify() {
        QueryWrapper<Weibo> qw = new QueryWrapper<>();
        qw.lambda().eq(Weibo::getState,2);
        List<Weibo> weibos = weiboMapper.selectList(qw);
        for(Weibo weibo:weibos){
            Generation gen = new Generation();

            String prompt = "角色说明：您是微博审核员。\n" +
                    "任务描述：我将为您提供一个标题和内容，您需要检查其中是否包含敏感或非法的词汇。\n" +
                    "审核结果：请直接回复“合法”、“非法”或“有待审核”。\n" +
                    "示例：\n" +
                    "  - 如果我说：“蠢货”，您应回复：“非法”。\n" +
                    "  - 如果我说：“好的”，您应回复：“合法”。\n" +
                    "请按照以上规则进行审核。";
            String input = weibo.getTitle() +" "+ weibo.getContent();
            System.out.println(input);
            QwenParam params = QwenParam.builder().model("qwen-turbo")
                    .prompt(prompt+",用户说:"+input)
                    .seed(1234)
                    .topP(0.8)
                    .resultFormat("message")
                    .enableSearch(false)
                    .maxTokens(100)
                    .temperature((float) 0.85)
                    .repetitionPenalty((float) 1.0)
                    .build();
            GenerationResult result = null;
            try {
                result = gen.call(params);
                System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
                if (result.getOutput().getChoices().get(0).getMessage().getContent().contains("合法")){
                    weibo.setState(1);
                }else if (result.getOutput().getChoices().get(0).getMessage().getContent().contains("非法")){
                    weibo.setState(-1);
                }else if(result.getOutput().getChoices().get(0).getMessage().getContent().contains("有待审核")){
                    weibo.setState(3);
                }
                weiboMapper.updateById(weibo);
            } catch (NoApiKeyException e) {
                e.printStackTrace();
            } catch (InputRequiredException e) {
                e.printStackTrace();
            }
        }
        for (int i=0;i<weibos.size();++i){
            System.out.println(weibos.get(i).getContent()+" 审核结果为："+weibos.get(i).getState());
        }
        return new Result<>(4000,weibos,"审核完成");
    }
}
